package com.projectName.www.service;


import com.projectName.www.po.message;
import com.projectName.www.util.String_util;
import com.projectName.www.util.find;
import com.projectName.www.dao.S_J_selectmessage_dao;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.util.Vector;

/**
     * @author 86132
     *
     */
    public class S_J_selectmessage extends JInternalFrame {
        private JTable message;
        private JTable mymessage;
        private JTextArea detail;
        private JLabel lblNewLabel;
        private JButton delete;
        private JButton insert;
        message m=new message();
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        S_J_selectmessage frame = new S_J_selectmessage();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the frame.
         */
        public S_J_selectmessage() throws Exception {
            setMaximizable(true);
            setClosable(true);
            setBounds(100, 100, 732, 593);

            JScrollPane scrollPane = new JScrollPane();

            message = new JTable();
            message.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                    },
                    new String[] {
                            "\u5BA2\u6237ID", "\u7559\u8A00"
                    }
            ));
            message.getColumnModel().getColumn(0).setPreferredWidth(112);
            message.getColumnModel().getColumn(1).setPreferredWidth(510);
            message.getColumnModel().getColumn(1).setMinWidth(24);
            scrollPane.setViewportView(message);

            JScrollPane scrollPane_1 = new JScrollPane();

            detail = new JTextArea();

            lblNewLabel = new JLabel("1.选中查看详细信息  2.添加“我的留言”");

            delete = new JButton("删除");
            delete.addActionListener(e->
            {
                try {
                    delete(e);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });
            insert = new JButton("添加");
            insert.addActionListener(e->
            {
                try {
                    insert(e);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });
            GroupLayout groupLayout = new GroupLayout(getContentPane());
            groupLayout.setHorizontalGroup(
                    groupLayout.createParallelGroup(Alignment.TRAILING)
                            .addGroup(groupLayout.createSequentialGroup()
                                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                            .addComponent(scrollPane_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 848, Short.MAX_VALUE)
                                            .addComponent(scrollPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 848, Short.MAX_VALUE)
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addContainerGap()
                                                    .addComponent(detail, GroupLayout.PREFERRED_SIZE, 436, GroupLayout.PREFERRED_SIZE)
                                                    .addGap(76)
                                                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                            .addComponent(insert, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(delete, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)))
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addGap(44)
                                                    .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 367, GroupLayout.PREFERRED_SIZE)))
                                    .addContainerGap())
            );
            groupLayout.setVerticalGroup(
                    groupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(groupLayout.createSequentialGroup()
                                    .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
                                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addGap(6)
                                                    .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(ComponentPlacement.RELATED)
                                                    .addComponent(detail, GroupLayout.PREFERRED_SIZE, 245, GroupLayout.PREFERRED_SIZE))
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addGap(79)
                                                    .addComponent(delete, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                                                    .addGap(52)
                                                    .addComponent(insert, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)))
                                    .addGap(16))
            );

            mymessage = new JTable();
            mymessage.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null},
                            {null},
                            {null},
                            {null},
                    },
                    new String[] {
                            "\u6211\u7684\u7559\u8A00"
                    }
            ));
            mymessage.getColumnModel().getColumn(0).setPreferredWidth(411);
            scrollPane_1.setViewportView(mymessage);
            getContentPane().setLayout(groupLayout);
            this.fillmymessage();
            this.fillmessage();
           mymessage.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    //按表编辑
                    int id =1;
                    int i=mymessage.getSelectedRow();
                    int j=0;
                    filldetail(id,i,j);

                }
            });
            message.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    //按表编辑
                    int id =0;
                    int i=message.getSelectedRow();
                    int j=message.getSelectedColumnCount();
                    filldetail(id,i,j);

                }
            });
        }
    //删除留言
    private void delete(ActionEvent e) throws Exception {
           boolean result=S_J_selectmessage_dao.deletemessage( detail.getText());
           if(result)
           {
               JOptionPane.showMessageDialog(null,"删除成功");
               fillmymessage();
               fillmessage();
           }
           else JOptionPane.showMessageDialog(null,"删除失败:没有该留言");
    }
    //填充详细信息
    private void filldetail(int id,int i, int j) {

        if (id == 1) {
            detail.setText((String) mymessage.getValueAt(i, j));
        }
        else {
            detail.setText((String) message.getValueAt(i,j));
        }
    }
    //添加店家留言
    private void insert(ActionEvent e) throws Exception {
            if(String_util.isEmpty(detail.getText()))
            {
                JOptionPane.showMessageDialog(null,"添加失败：留言不能为空");
                return;
            }
           m.setMessage(detail.getText());
           m.setShopname(find.name(OpenBusiness.username));
            boolean result=S_J_selectmessage_dao.insert(m);
            if(result)
            {
                JOptionPane.showMessageDialog(null,"添加成功");
                fillmymessage();
            }
            else JOptionPane.showMessageDialog(null,"添加失败");
        }
    //填充我的留言
    public  void fillmymessage() throws Exception {
        DefaultTableModel model = (DefaultTableModel) mymessage.getModel();
        model.setRowCount(0);//设置为0行
        ResultSet f = S_J_selectmessage_dao.getmessage();
        while (f.next()) {
            if(!String_util.isEmpty(f.getString("buyers")))
                continue;

                Vector vector = new Vector();
                vector.add(f.getString("message"));

                model.addRow(vector);

        }
    }
    //填充客户留言
    public  void fillmessage() throws Exception {
        DefaultTableModel model = (DefaultTableModel)message.getModel();
        model.setRowCount(0);//设置为0行
        ResultSet f = S_J_selectmessage_dao.getmessage();
        while (f.next())
        {
            if(String_util.isEmpty(f.getString("buyers")))
                continue;
            Vector v=new Vector();

            v.add(f.getString("buyers"));
            v.add(f.getString("message"));

            model.addRow(v);


    }
    }


   }


